package com.dz.controller.user;

import com.dz.constant.MessageConstant;
import com.dz.dto.UserLoginDTO;
import com.dz.properties.WeChatProperties;
import com.dz.result.Result;
import com.dz.service.UserService;
import com.dz.vo.UserLoginVO;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 鄧浩奇
 * @classname UserController
 * @description
 * @since 2024/1/6 17:44
 **/


@Api(tags = "用户操作接口")
@RestController
@RequestMapping("/user/user")
public class UserController {

    private final UserService userService;
//    private final WeChatProperties weChatProperties;

    @Autowired
    public UserController(UserService userService, WeChatProperties weChatProperties) {
        this.userService = userService;
//        this.weChatProperties = weChatProperties;
    }

    @ApiOperation("微信登陆")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        Assert.notNull(userLoginDTO, MessageConstant.USER_CODE_IS_NULL);
        UserLoginVO userLoginVO = userService.wxLogin(userLoginDTO.getCode());
  /*      // 获取微信小程序接口调用凭证
        WechatAccess access = getAccess();
        access.setOpenid(userLoginVO.getOpenid());

        // 获取用户信息
        User userInfo = getUserInfo(access);*/

        return Result.success(userLoginVO);
    }


   /* // 获取接口调用凭证
    private WechatAccess getAccess() {
        Map<String,String> paramMap = Map.of(
                "grant_type", "client_credential",
                "appid", weChatProperties.getAppid(),
                "secret", weChatProperties.getSecret()
        );
        String s = HttpClientUtil.doGet("https://api.weixin.qq.com/cgi-bin/token", paramMap);
        Gson gson = new Gson();
        return gson.fromJson(s, WechatAccess.class);
    }


    // 根据openid获取用户信息
    private User getUserInfo(WechatAccess access) {
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("access_token",access.getAccess_token());
        paramMap.put("openid",access.getOpenid());
        paramMap.put("lang","zh_CN");
        String s = HttpClientUtil.doGet("https://api.weixin.qq.com/sns/userinfo", paramMap);
        Gson gson = new Gson();
        return gson.fromJson(s,User.class);
    }*/
}
